package com.hsy.bibc.modules.main.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.ui.adapter.DPBaseAdapter;
import com.deparse.dpframe.ui.widget.DPCircleImageView;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.bibc.R;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.modules.code.zxing.MipcaActivityCapture;
import com.hsy.bibc.modules.common.CommonBaseFragment;
import com.hsy.bibc.modules.exchange.ExChangeActivity;
import com.hsy.bibc.modules.main.MainActivity;
import com.hsy.bibc.modules.main.gsondata.RecentRecordData;
import com.hsy.bibc.modules.order.OrderDetailActivity;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.utils.HttpUtil;
import com.hsy.bibc.utils.VolleyLoadPicture;

import java.util.List;

/**
 * Created by hsy on 2016/6/1.
 */
public class FragmentMain extends CommonBaseFragment {

    private static final int CMD_GET_RECENT_RECORDS = 0;

    private MyAdapter mMyAdapter;
    private ListView mListView;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            RecentRecordData.Result result = DPGsonUtil.fromJson(response, RecentRecordData.Result.class);

            showTitle(result);
            if (result.getOrder().size() == 0) {

                empty_linear.setVisibility(View.VISIBLE);

                mListView.setVisibility(View.GONE);
            } else {

                empty_linear.setVisibility(View.GONE);

                mListView.setVisibility(View.VISIBLE);

                List<RecentRecordData.Order> datas = result.getOrder();
                mMyAdapter = new MyAdapter(getAttchedActivity(), datas);
                mListView.setAdapter(mMyAdapter);
            }


            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            CommonUtils.hideProgressDialog();
        }
    };
    private View empty_linear;

    private void showTitle(final RecentRecordData.Result results) {

        TextView homeTitle = (TextView) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.home_title);
        homeTitle.setText(results.getShop_service().getName());

        RelativeLayout qr_scan_relative = (RelativeLayout) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.qr_scan_relative);
        RelativeLayout exchange_relative = (RelativeLayout) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.exchange_relative);
        RelativeLayout gathering_relative = (RelativeLayout) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.gathering_relative);
        qr_scan_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if (!"1".equals(results.getRole().getIs_fp())) {
                    showToast(R.string.no_permission);
                    return;
                }
                intent.setClass(getAttchedActivity(), MipcaActivityCapture.class);
                intent.putExtra(Consts.Keys.FROM_WHERE, "0");
                startActivity(intent);
            }
        });
        exchange_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!"1".equals(results.getRole().getIs_dh())) {
                    showToast(R.string.no_permission);
                    return;
                }
                startActivity(new Intent(getAttchedActivity(), ExChangeActivity.class));
            }
        });
        gathering_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if (!"1".equals(results.getRole().getIs_xf())) {
                    showToast(R.string.no_permission);
                    return;
                }
                intent.setClass(getAttchedActivity(), MipcaActivityCapture.class);
                intent.putExtra(Consts.Keys.FROM_WHERE, "1");
                startActivity(intent);
            }
        });

        TextView qr_scan = (TextView) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.qr_scan);
        TextView qr_scan_false = (TextView) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.qr_scan_false);
        TextView exchange = (TextView) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.exchange);
        TextView exchange_false = (TextView) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.exchange_false);
        TextView gathering = (TextView) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.gathering);
        TextView gathering_false = (TextView) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.gathering_false);
        TextView order = (TextView) ((MainActivity) getAttchedActivity()).getCurrentActionBar().findViewById(R.id.order);
        qr_scan.setVisibility("1".equals(results.getRole().getIs_fp()) ? View.VISIBLE : View.GONE);
        qr_scan_false.setVisibility("1".equals(results.getRole().getIs_fp()) ? View.GONE : View.VISIBLE);
        exchange.setVisibility("1".equals(results.getRole().getIs_dh()) ? View.VISIBLE : View.GONE);
        exchange_false.setVisibility("1".equals(results.getRole().getIs_dh()) ? View.GONE : View.VISIBLE);
        gathering.setVisibility("1".equals(results.getRole().getIs_xf()) ? View.VISIBLE : View.GONE);
        gathering_false.setVisibility("1".equals(results.getRole().getIs_xf()) ? View.GONE : View.VISIBLE);
        order.setVisibility(View.VISIBLE);
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mListView = findViewById(R.id.list_view);
        empty_linear = findViewById(R.id.stub_import);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                DPLogUtil.d("getOrder_id==" + mMyAdapter.getData(position).getOrder_id());
                DPLogUtil.d("getType==" + mMyAdapter.getData(position).getType());
                startActivity(OrderDetailActivity.class, mMyAdapter.getData(position).getOrder_id(), mMyAdapter.getData(position).getType());
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        CommonUtils.showProgressDialog(getAttchedActivity());
        HttpUtil.httpGet(getAttchedActivity(), CMD_GET_RECENT_RECORDS, Consts.Apis.GET_RECENT_RECORDS, callback);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onHandleMessage(Message msg) {

    }

    @Override
    public void onFragmentUpdate() {

    }


    class MyAdapter extends DPBaseAdapter<RecentRecordData.Order> {

        public MyAdapter(Context context, List<RecentRecordData.Order> data) {
            super(context, data);
        }

        @Override
        public CommonBaseViewHolder<RecentRecordData.Order> getHolder(int position) {
            return new ViewHolder();
        }

        class ViewHolder extends CommonBaseViewHolder<RecentRecordData.Order> {

            private View root;

            protected TextView orderTime;
            protected DPCircleImageView avtar;
            private TextView orderId;
            private TextView nickName;
            private TextView description;

            @Override
            public View create(int position) {
                root = inflate(R.layout.item_recent_records);
                initView(root);
                return root;
            }

            @Override
            public void update(int position, RecentRecordData.Order data) {
                orderId.setText(data.getType_name() + ": " + data.getOrder_sn());
                nickName.setText(data.getUser().getNickname());
                description.setText(data.getScore() + "积分");
                orderTime.setText(data.getCreate_datetime_str());
//                DPPicassoUtil.load(context, data.getUser().getAvatar(), avtar);
                VolleyLoadPicture vlp = new VolleyLoadPicture(context, avtar);
                vlp.getmImageLoader().get(data.getUser().getAvatar(), vlp.getOne_listener());
            }

            private void initView(View rootView) {
                orderId = (TextView) rootView.findViewById(R.id.order_id);
                nickName = (TextView) rootView.findViewById(R.id.nick_name);
                description = (TextView) rootView.findViewById(R.id.description);
                orderTime = (TextView) rootView.findViewById(R.id.order_time);
                avtar = (DPCircleImageView) rootView.findViewById(R.id.avtar);
            }
        }
    }

}
